#include<stdio.h>
#include"lcd.h"
#include"bmp.h"
#include"touch.h"
#include"music.h"
#include"video.h"
//相册
void photo(LCD *lcd,TouchScreen *ts)
{
    char *bmp[7] = {
        "image/1.bmp",
        "image/2.bmp",
        "image/3.bmp",
        "image/4.bmp",
        "image/5.bmp",
        "image/6.bmp",
        "image/7.bmp",
    };

    Coordinate coor;

    int index = 0;
    anybmp(0,0,bmp[index],lcd);
    while(1)
    {
        //获取坐标
        ts_press(&coor,ts);
        printf("(%d,%d)\n",coor.x,coor.y);
        //退出
        if(coor.y >400 && coor.y<480)
        {
            break;
        }else if(coor.x>0 && coor.x<400)//上一张
        {
            index = (--index == -1)? 6:index;
        }else if(coor.x>400 && coor.x<800)//下一张
        {
            index = (++index == 7)? 0:index;
        }
        anybmp(0,0,bmp[index],lcd);
    }
}

void music(LCD *lcd,TouchScreen *ts)
{
    char music_file[4][256] = {"music/1.mp3", "music/2.mp3", "music/3.mp3", "music/4.mp3" };
    char *music_bmp[2] = {"image/music2.bmp","image/music1.bmp"};//400 240
    //150 100 
    Coordinate coor;
    int index=0;
    int b_index=1;
    int stop_flag=1;
    anybmp(0,0,"image/ff.bmp",lcd);
    anybmp(200,120,music_bmp[0],lcd);
    //保证madplay是关闭的
    kill_madplay();
    start_madplay(music_file[index]);
    stop_madplay();
    //循环播放音乐
    while(1)
    {
        ts_press(&coor,ts);
        printf("(%d,%d)\n",coor.x,coor.y);
        //退出
        if(coor.y >440 && coor.y<480)
        {
            break;
        }else if(coor.x>285 && coor.x<320 && coor.y>250 && coor.y< 280)//上一首
        {
            printf("上一首\n");
            //stop_madplay();
            index = (--index == -1)? 4:index;
            kill_madplay();
            start_madplay(music_file[index]);
        }else if(coor.x>475 && coor.x<510 && coor.y>250 && coor.y< 280)//下一首
        {
            printf("下一首\n");
            //stop_madplay();
            index = (++index == 4)? 0:index;
            kill_madplay();
            start_madplay(music_file[index]);
        }else if(coor.x>353 && coor.x<445 && coor.y>220 && coor.y< 315){
            b_index= (b_index+1)%2;
            anybmp(200,120,music_bmp[b_index],lcd);
            if(b_index == 0)
            {
                printf("stop music\n");
                stop_madplay();
                stop_flag=1;
            }else{
                if(stop_flag==1)
                {
                    printf("continue music\n");
                    continue_madpaly();
                    stop_flag=0;
                }
            }
        }
    }
}

void video(LCD *lcd,TouchScreen *ts)
{
    char video_file[4][256] = {"video/1.avi","video/2.avi","video/3.avi","video/4.avi"};
    char *video_bmp[2] = {"image/video1.bmp","image/video2.bmp"};//800*80

    Coordinate coor;
    int index=0;
    int b_index=0;
    int stop_flag=1;
    anybmp(0,0,"image/ff.bmp",lcd);
    //保证mplayer是关闭的
    kill_mplayer();
    start_mplayer(video_file[index]);
    anybmp(0,400,video_bmp[b_index],lcd);
    // start_mplayer(video_file[index]);
    stop_mplayer();
    //视频
    while(1)
    {
        // start_mplayer(video_file[index]);
        // anybmp(0,400,video_bmp[b_index],lcd);

        printf("index=%s\n",video_file[index]);
        ts_press(&coor,ts);
        printf("(%d,%d)\n",coor.x,coor.y);
        //退出
        if(coor.x >700 && coor.x<800 && coor.y >0 && coor.y<100)
        {
            stop_mplayer();
            kill_mplayer();
            break;
        }else if(coor.x>30 && coor.x<70 && coor.y>440 && coor.y< 480)//上一首
        {
            printf("上一部\n");
            index = (--index == -1)? 4:index;
            stop_mplayer();
            kill_mplayer();
            
            start_mplayer(video_file[index]);
            b_index=1;
            anybmp(0,400,video_bmp[b_index],lcd);
        }else if(coor.x>130 && coor.x<170 && coor.y>440 && coor.y< 480)//下一首
        {
            printf("下一部\n");
            index = (++index == 4)? 0:index;
            stop_mplayer();
            kill_mplayer();
            start_mplayer(video_file[index]);
            b_index=1;
            anybmp(0,400,video_bmp[b_index],lcd);
        }else if(coor.x>70 && coor.x<120 && coor.y>430 && coor.y< 480){
            b_index= (b_index+1)%2;
            //anybmp(0,0,"image/ff.bmp",lcd);
            anybmp(0,400,video_bmp[b_index],lcd);
            if(b_index == 0)
            {
                stop_mplayer();
                stop_flag=1;
            }else{
                if(stop_flag==1){
                    continue_mplayer();
                }
            }
        }
        //anybmp(0,0,"image/ff.bmp",lcd);
        //anybmp(0,400,video_bmp[b_index],lcd);
        //printf("flag=%d\n",flag);
        // if(flag == 1)
        // {
        //     stop_mplayer();
        //     kill_mplayer();
        //     start_mplayer(video_file[index]);
        // }
        // if(b_index == 0)
        // {
        //     printf("stop video\n");
        //     printf("index=%d\n",index);
        //     stop_mplayer();
        // }else{
        //     printf("continue video\n");
        //     printf("index=%d\n",index);
        //     stop_mplayer();
        //     continue_mplayer();
        // }
    }
}

int main(int argc, char const *argv[])
{
    LCD lcd;
    int x,y;

    //初始化
    lcd_init(&lcd);
    Coordinate coor;
    TouchScreen ts;

    touchscreen_init(&ts);

    //系统逻辑
    while(1)
    {
        printf("相册、音乐、视频\n");
        anybmp(0,0,"res/menu.bmp",&lcd);
        
        ts_press(&coor,&ts);
        printf("(%d,%d)\n",coor.x,coor.y);
        if(coor.x>178 && coor.x< 276 && coor.y > 136 && coor.y<220)
        {
            anybmp(177,136,"res/xcx.bmp",&lcd);//未松开之前,有阴影
            ts_loosen(&coor,&ts);//松开,更新坐标
            anybmp(177,136,"res/xc.bmp",&lcd);

            //仍处于相册范围内，则进入相册
            if(coor.x>176 && coor.x< 276 && coor.y > 136 && coor.y<220)
            {
                printf("相册\n");
                photo(&lcd,&ts);
            }
        }
        else if(coor.x>360 && coor.y>136 && coor.x<449 && coor.y<225)
        {
            // anybmp(361,136,"res/yyx.bmp",&lcd);
            // ts_loosen(&coor,&ts);
            // anybmp(361,136,"res/yy.bmp",&lcd);
            // if(coor.x>360 && coor.y>136 && coor.x<449 && coor.y<225)
            // {
                //进入音乐
                printf("音乐\n");
                music(&lcd,&ts);
            // }
        }
        else if(coor.x>549 && coor.y>135 && coor.x<646 && coor.y<225)
        {
            // anybmp(549, 134, "res/spx.bmp", &lcd);
            // ts_loosen(&coor, &ts);
            // anybmp(549, 135, "res/sp.bmp", &lcd);
            // if(coor.x>549 && coor.y>135 && coor.x<646 && coor.y<225)
            // {
                printf("视频\n");
                video(&lcd,&ts);
            // }
        }
    }

    //释放液晶屏资源
    lcd_uninit(&lcd);
    touchscreen_uninit(&ts);
    return 0;

}